package io.renren.modules.examination.controller;

import io.renren.common.utils.R;
import io.renren.modules.examination.entity.AnswerEntity;
import io.renren.modules.examination.entity.ExaminationQuestionsEntity;
import io.renren.modules.examination.entity.OptionEntity;
import io.renren.modules.examination.entity.QuestionsEntity;
import io.renren.modules.examination.service.ExaminationService;
import io.renren.modules.sys.dto.AnswerDto;
import io.renren.modules.sys.service.TokenService;
import io.renren.modules.sys.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/api")
@Api(tags="测评接口")
public class ExaminationController {
    @Autowired
    ExaminationService examinationService;

    @Autowired
    TokenService tokenService;

    @Autowired
    UserService userService;


    /**
     * 获取试卷列表
     */
    @PostMapping("getExaminationPaperList")
    @ApiOperation("查看试卷列表")
    public R  getExaminationPaperList(){
        List examinationPaperList = examinationService.getExaminationPaperList();
        return R.ok("获取试卷列表成功",100).put("data",examinationPaperList);
    }

    /**
     * 获取当前试卷题目列表
     */
    @PostMapping("getExaminationQuestionsList")
    @ApiOperation("查看当前试卷题目")
    public R getExaminationQuestionsList(@RequestParam(value = "paperId") Integer paperId){
        List questionsList = examinationService.getExaminationQuestionsList(paperId);
        if(questionsList.size()>0){
            List<QuestionsEntity> list = new ArrayList();
            for (int i=0;i<questionsList.size();i++) {
                ExaminationQuestionsEntity examinationQuestionsEntity = (ExaminationQuestionsEntity) questionsList.get(i);
                QuestionsEntity questionsEntity = new QuestionsEntity();
                questionsEntity.setId(examinationQuestionsEntity.getId());
                questionsEntity.setQuestion(examinationQuestionsEntity.getQuestion());
                List<OptionEntity> optionList = new ArrayList();
                switch (1) {
                    case 1 :
                        OptionEntity optionEntity1 = new OptionEntity();
                        optionEntity1.setOptionContent(examinationQuestionsEntity.getOption1());
                        optionEntity1.setOptionNum(1);
                        optionList.add(optionEntity1);
                    case 2:
                        OptionEntity optionEntity2 = new OptionEntity();
                        optionEntity2.setOptionContent(examinationQuestionsEntity.getOption2());
                        optionEntity2.setOptionNum(2);
                        optionList.add(optionEntity2);
                    case 3:
                        if (examinationQuestionsEntity.getOption3()!=null) {
                            OptionEntity optionEntity3 = new OptionEntity();
                            optionEntity3.setOptionContent(examinationQuestionsEntity.getOption3());
                            optionEntity3.setOptionNum(3);
                            optionList.add(optionEntity3);
                        } else
                            break;
                    case 4:
                        if (examinationQuestionsEntity.getOption4()!=null) {
                            OptionEntity optionEntity4 = new OptionEntity();
                            optionEntity4.setOptionContent(examinationQuestionsEntity.getOption4());
                            optionEntity4.setOptionNum(4);
                            optionList.add(optionEntity4);
                        } else
                            break;
                    case 5:
                        if (examinationQuestionsEntity.getOption5()!=null) {
                            OptionEntity optionEntity5 = new OptionEntity();
                            optionEntity5.setOptionContent(examinationQuestionsEntity.getOption5());
                            optionEntity5.setOptionNum(5);
                            optionList.add(optionEntity5);
                        } else
                            break;
                }
                questionsEntity.setOptionEntityList(optionList);
                list.add(questionsEntity);
            }
            return R.ok("查询题目成功",100).put("data",list);
        }
        return R.ok("此试卷尚无题目",100).put("data",questionsList);
    }

    /**
     * 获取测评结果
     */
    @PostMapping("getExaminationResultType")
    @ApiOperation("获取测评结果")
    public R getExaminationResultType(@RequestBody AnswerDto answerDto){

        /*
        JSONObject data = JSONObject.fromObject(answerListJson);
        JSONArray answerList = data.getJSONArray("answerList");
        for(int i=0;i<answerList.size();i++){
            String s = answerList.getString(i);
            JSONObject answer = JSONObject.fromObject(s);
            answer.getInt("questionId");
            answer.getInt("answer");
        }
        */
        List<AnswerEntity> answerList = answerDto.getAnswerList();
        AnswerEntity answerEntity = answerList.get(0);
        List<ExaminationQuestionsEntity> questionsList = examinationService.getExaminationQuestionsListByQuestionId(answerEntity.getQuestionId());
        Map map = new HashMap<String, Integer>();
        String rid = "";
        int sc = 0;
        for (ExaminationQuestionsEntity examinationQuestionsEntity:questionsList) {
            for (int i=0;i<answerList.size();i++) {
                answerEntity = answerList.get(i);
                if (examinationQuestionsEntity.getId() == answerEntity.getQuestionId()) {
                    if (answerEntity.getAnswer() == 0) {
                        break;
                    } else if (answerEntity.getAnswer() == 1) {
                        rid = String.valueOf(examinationQuestionsEntity.getOption1ResultTypeId());
                        sc = examinationQuestionsEntity.getOption1Score();
                    } else if (answerEntity.getAnswer() == 2) {
                        rid = String.valueOf(examinationQuestionsEntity.getOption2ResultTypeId());
                        sc = examinationQuestionsEntity.getOption2Score();
                    } else if (answerEntity.getAnswer() == 3) {
                        rid = String.valueOf(examinationQuestionsEntity.getOption3ResultTypeId());
                        sc = examinationQuestionsEntity.getOption3Score();
                    } else if (answerEntity.getAnswer() == 4) {
                        rid = String.valueOf(examinationQuestionsEntity.getOption4ResultTypeId());
                        sc = examinationQuestionsEntity.getOption4Score();
                    } else if (answerEntity.getAnswer() == 5) {
                        rid = String.valueOf(examinationQuestionsEntity.getOption5ResultTypeId());
                        sc = examinationQuestionsEntity.getOption5Score();
                    }

                    if (map.containsKey(rid)) {
                        sc = (Integer) map.get(rid) + sc;
                        map.put(rid, sc);
                    } else {
                        map.put(rid, sc);
                    }
                    break;
                }
            }
        }
        rid = "";
        sc = 0;
        Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> entry = iterator.next();
            if (entry.getValue()>sc) {
                    rid = entry.getKey();
                    sc = entry.getValue();
            } else if (entry.getValue().equals(sc) && sc!=0) {
                rid = rid + "," + entry.getKey();
            }
        }
        List<String> resultTypeIdList = new ArrayList();
        if (rid.contains(",")) {
            String[] tmp = rid.split(",");
            for (String s:tmp) {
                resultTypeIdList.add(s);
            }
        } else {
            resultTypeIdList.add(rid);
        }
        List resultList = examinationService.getExaminationResultTypeList(resultTypeIdList);
        return R.ok("查询结果成功",100).put("data",resultList);
    }

}
